//
//  SepLineView.m
//  CareRingApp
//
//  Created by Linktop on 2024/4/15.
//  分割线

#import "SepLineView.h"

@implementation SepLineView
{
    __weak CAShapeLayer *_lineLayer;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initData];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initData];
    }
    return self;
}

-(void)initData {
    
    self.leftPadding = 0;
    self.rightPadding = 0;
    self.lineWidth = 0.5f;
    self.lineColor = [UIColor lightGrayColor];
    
    self.dashPattern = @[@(3), @(5)];
    
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    [self drawSep];
    
}

-(void)drawSep {
    if (_lineLayer) {
        [_lineLayer removeFromSuperlayer];
    }
 
    
    CGFloat width = self.bounds.size.width;
    CGFloat height = self.bounds.size.height;
  
    UIBezierPath *gridPath = [UIBezierPath bezierPath];
    
    [gridPath moveToPoint:CGPointMake(0+self.leftPadding, height/2)];
    [gridPath addLineToPoint:CGPointMake(width - self.rightPadding, height/2)];

    CAShapeLayer *lineLayer = [CAShapeLayer layer];
    lineLayer.frame = self.bounds;
    lineLayer.lineWidth = self.lineWidth;
  
    
    lineLayer.strokeColor = self.lineColor.CGColor;
    lineLayer.lineCap = kCALineCapSquare;
    if (self.isDash) {
        [lineLayer setLineDashPattern:self.dashPattern];
    }
    lineLayer.path = gridPath.CGPath;
    [self.layer addSublayer:lineLayer];
    _lineLayer = lineLayer;
}

@end
